package org.ww.edu.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.ww.commonutils.R;
import org.ww.edu.bean.Course;
import org.ww.edu.bean.vo.CourseInfoVo;
import org.ww.edu.bean.vo.CoursePublishVo;
import org.ww.edu.bean.vo.CourseQuery;
import org.ww.edu.service.CourseService;

import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author ww
 * @since 2020-10-23
 */
@RestController
@RequestMapping("/edu/course")
@Api(description = "课程管理")
public class CourseController {
    @Autowired
    private CourseService courseService;

    //1 添加课程基本信息
    @PostMapping("/addCourseInfo")
    public R addCourseInfo(@RequestBody CourseInfoVo courseInfoVo){
        String id=courseService.addCourseInfo(courseInfoVo);
        return R.ok().data("courseId",id);
    }

    //2 根据id查询课程信息
    @GetMapping("getCourseInfo/{courseId}")
    public R getCourseInfo(@PathVariable String courseId){
        CourseInfoVo courseInfoVo=courseService.getCourseInfo(courseId);
        return R.ok().data("courseInfo",courseInfoVo);
    }

    //3 根据id修改课程信息
    @PostMapping("/updateCourseInfo")
    public R updateCourseInfo(@RequestBody CourseInfoVo courseInfoVo){
        courseService.updateCourseInfo(courseInfoVo);
        return R.ok();
    }

    //4 查看发布课程信息
    @GetMapping("/getPublishCourseInfo/{courseId}")
    public R getPublishCourseInfo(@PathVariable String courseId){
        CoursePublishVo coursePublishVo=courseService.getPublishCourseInfo(courseId);
        return R.ok().data("coursePublishInfo",coursePublishVo);
    }
    //5 发布课程信息
    @GetMapping("/publishCourse/{courseId}")
    public R publishCourse(@PathVariable String courseId){
        Course course=new Course();
        course.setId(courseId);
        course.setStatus("Normal");
        courseService.updateById(course);
        return R.ok();
    }

    //6 多条件分页查询课程列表
    @PostMapping("/pageCourse/{page}/{limit}")
    public R pageCourse(@PathVariable long page, @PathVariable long limit,
                        @RequestBody CourseQuery courseQuery){
        IPage<Course> courseIPage = courseService.pageCourse(page, limit, courseQuery);
        long total = courseIPage.getTotal();
        return R.ok().data("pageCourse",courseIPage.getRecords()).data("total",total);
    }

    //7 根据课程id删除课程信息
    @DeleteMapping("/{courseId}")
    public R deleteCourse(@PathVariable String courseId){
        courseService.deleteCourse(courseId);
        return R.ok();
    }

}

